home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / LSOPEN.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  114 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)lsopen.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STRING
  14. #include <string.h>
  15. #endif
  16.  
  17. /* library headers */
  18. #include <blkio.h>
  19.  
  20. /* local headers */
  21. #include "lseq_.h"
  22.  
  23. /* lseq control structure table definition */
  24. lseq_t lsb[LSOPEN_MAX];
  25.  
  26. /*man---------------------------------------------------------------------------
  27. NAME
  28.      lsopen - open an lseq
  29.  
  30. SYNOPSIS
  31.      lseq_t *lsopen(filename, type)
  32.      const char *filename;
  33.      const char *type;
  34.  
  35. DESCRIPTION
  36.      The lsopen function opens the file named by filename as an lseq.
  37.      A pointer to the lseq_t structure associated with the file is
  38.      returned.
  39.  
  40.      type is a character string having one of the following values:
  41.  
  42.           "r"            open for reading
  43.           "r+"           open for update (reading and writing)
  44.  
  45.      lsopen will fail if one or more of the following is true:
  46.  
  47.      [EINVAL]       filename is the NULL pointer.
  48.      [EINVAL]       type is not "r" or "r+".
  49.      [ENOENT]       The named lseq file does not exist.
  50.      [LSEMFILE]     Too many open lseqs.  The maximum
  51.                     is defined as LSOPEN_MAX in lseq.h.
  52.  
  53. SEE ALSO
  54.      lsclose, lscreate.
  55.  
  56. DIAGNOSTICS
  57.      lsopen returns a NULL pointer on failure, and errno is set to
  58.      indicate the error.
  59.  
  60. ------------------------------------------------------------------------------*/
  61. #ifdef AC_PROTO
  62. lseq_t *lsopen(const char *filename, const char *type)
  63. #else
  64. lseq_t *lsopen(filename, type)
  65. const char *filename;
  66. const char *type;
  67. #endif
  68. {
  69.     lseq_t *    lsp    = NULL;
  70.  
  71.     /* validate input parameters */
  72.     if (filename == NULL || type == NULL) {
  73.         errno = EINVAL;
  74.         return NULL;
  75.     }
  76.  
  77.     /* find free slot in lsb table */
  78.     for (lsp = lsb; lsp < lsb + LSOPEN_MAX; ++lsp) {
  79.         if (!(lsp->flags & LSOPEN)) {
  80.             break;        /* found */
  81.         }
  82.     }
  83.     if (lsp >= lsb + LSOPEN_MAX) {
  84.         errno = LSEMFILE;
  85.         return NULL;        /* no free slots */
  86.     }
  87.  
  88.     /* open file */
  89.     if (strcmp(type, LS_READ) == 0) {
  90.         lsp->flags = LSREAD;
  91.     } else if (strcmp(type, LS_RDWR) == 0) {
  92.         lsp->flags = LSREAD | LSWRITE;
  93.     } else {
  94.         errno = EINVAL;
  95.         return NULL;
  96.     }
  97.     lsp->bp = bopen(filename, type, sizeof(lshdr_t), (size_t)1, (size_t)0);
  98.     if (lsp->bp == NULL) {
  99. #ifdef DEBUG
  100.         if (errno != EACCES && errno != ENOENT) LSEPRINT;
  101. #endif
  102.         memset(lsp, 0, sizeof(*lsp));
  103.         lsp->flags = 0;
  104.         return NULL;
  105.     }
  106.  
  107.     /* load lseq_t structure */
  108.     memset(&lsp->lshdr, 0, sizeof(lsp->lshdr));    /* header */
  109.     lsp->clspos = NIL;            /* cursor */
  110.     lsp->clsrp = NULL;            /* current record pointer */
  111.  
  112.     return lsp;
  113. }
  114.